using System;
using System.Threading.Tasks;
using JlPay.Sdk.Core;
using JlPay.Sdk.Models.Openmerch.Change;

namespace JlPay.Sdk.Services.Openmerch.Change
{
    /// <summary>
    /// 开放商户变更服务
    /// </summary>
    public class ChangeService : BaseService
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="config">配置对象</param>
        public ChangeService(Config config) : base(config)
        {
        }

        /// <summary>
        /// 获取API URL
        /// </summary>
        /// <param name="requestClass">请求类</param>
        /// <param name="apiPath">API路径</param>
        /// <returns>完整的API URL</returns>
        private string GetApiUrl(Type requestClass, string? apiPath = null)
        {
            var baseUrl = _config.BaseUrl;

            if (!string.IsNullOrEmpty(apiPath))
            {
                return baseUrl + apiPath;
            }

            // 使用请求类中定义的API_URL
            var apiUrlField = requestClass.GetField("API_URL");
            if (apiUrlField != null)
            {
                var apiUrl = apiUrlField.GetValue(null) as string;
                if (!string.IsNullOrEmpty(apiUrl))
                {
                    return baseUrl + apiUrl;
                }
            }

            // 如果没有定义API_URL，使用默认路径
            return baseUrl + "/open/merch/access";
        }

        /// <summary>
        /// 变更费率信息
        /// </summary>
        /// <param name="request">请求对象</param>
        /// <returns>响应对象</returns>
        public async Task<RateInfoChangeResponse> ChangeRateInfoAsync(RateInfoChangeRequest request)
        {
            return await PostAsync<RateInfoChangeRequest, RateInfoChangeResponse>(request, RateInfoChangeRequest.API_URL);
        }

        /// <summary>
        /// 变更商户信息
        /// </summary>
        /// <param name="request">请求对象</param>
        /// <returns>响应对象</returns>
        public async Task<MerchInfoChangeResponse> ChangeMerchInfoAsync(MerchInfoChangeRequest request)
        {
            return await PostAsync<MerchInfoChangeRequest, MerchInfoChangeResponse>(request, MerchInfoChangeRequest.API_URL);
        }

        /// <summary>
        /// 变更结算账户
        /// </summary>
        /// <param name="request">请求对象</param>
        /// <returns>响应对象</returns>
        public async Task<SettleAccountChangeResponse> ChangeSettleAccountAsync(SettleAccountChangeRequest request)
        {
            return await PostAsync<SettleAccountChangeRequest, SettleAccountChangeResponse>(request, SettleAccountChangeRequest.API_URL);
        }

        /// <summary>
        /// 修改固定提现
        /// </summary>
        /// <param name="request">请求对象</param>
        /// <returns>响应对象</returns>
        public async Task<FixedWithdrawModifyResponse> ModifyFixedWithdrawAsync(FixedWithdrawModifyRequest request)
        {
            return await PostAsync<FixedWithdrawModifyRequest, FixedWithdrawModifyResponse>(request, FixedWithdrawModifyRequest.API_URL);
        }

        /// <summary>
        /// 修改签约信息
        /// </summary>
        /// <param name="request">请求对象</param>
        /// <returns>响应对象</returns>
        public async Task<SignInfoModifyResponse> ModifySignInfoAsync(SignInfoModifyRequest request)
        {
            return await PostAsync<SignInfoModifyRequest, SignInfoModifyResponse>(request, SignInfoModifyRequest.API_URL);
        }

        /// <summary>
        /// 修改结算规则
        /// </summary>
        /// <param name="request">请求对象</param>
        /// <returns>响应对象</returns>
        public async Task<SettleRuleModifyResponse> ModifySettleRuleAsync(SettleRuleModifyRequest request)
        {
            return await PostAsync<SettleRuleModifyRequest, SettleRuleModifyResponse>(request, SettleRuleModifyRequest.API_URL);
        }
    }
} 